package cn.rengy.web.framework.wxmp.token;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import cn.rengy.tpa.wxmp.token.AccessTokenApi;
import cn.rengy.tpa.wxmp.token.AccessTokenResponse;
import cn.rengy.web.framework.wxmp.WxApp;
import cn.rengy.web.framework.wxmp.WxAppService;
import lombok.extern.slf4j.Slf4j;
@ConditionalOnProperty(name = "spring.cache.type", havingValue = "redis", matchIfMissing = false)
@Service
@Slf4j
public class AccessTokenRefreshService extends AbstractTokenRefreshService{
	@Autowired
	private WxAppService wxAppService;
	

	@Override
	TokenDTO newToken(String appid) {
		TokenDTO tokenDTO=new TokenDTO();
		WxApp wxApp=wxAppService.getApp(appid);
		String appsecret=wxApp.getAppsecret();
		AccessTokenResponse accessTokenResponse=AccessTokenApi.token(appid,appsecret);
		tokenDTO.setExpires(accessTokenResponse.getExpires_in());
		tokenDTO.setToken(accessTokenResponse.getAccess_token());
		return tokenDTO;
	}

	@Override
	void afterSaveToken() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String getLockKey(String appid) {
		return TokenKeyService.getAccessTokenLockKey(appid);
	}

	@Override
	public String getAccessKey(String appid) {
		return TokenKeyService.getAccessTokenKey(appid);
	}

}
